$(function () {
    $("#jqGrid").Grid({
        url: '../storehouse/list',
        colModel: [
            {label: '仓库代码', name: 'storeCode', index: 'STORE_CODE', width: 80},
            {label: '仓库名称', name: 'name', index: 'NAME', width: 80},
            {
                label: '仓库类型', name: 'storeType', index: 'STORE_TYPE', width: 80, formatter: function (value) {
                    return sysEnums.STORE_TYPE[value];
                }
            },
            {
                label: '模块类型', name: 'moduleType', index: 'MODULE_TYPE', width: 80, formatter: function (value) {
                    return sysEnums.STORE_TYPE_2[value];
                }
            },
            {
                label: '所属车间', name: 'workshopName', index: 'WORKSHOP', width: 80,
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '业务分管', width: 40, align: 'center', sortable: false, formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.goUser(\"" + row.id + "\")'><i class='fa fa-user'></i>&nbsp;分管人员</button>";
                }
            }
        ]
    });
    vm.getStore();
});
var ztree;

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
    callback: {
        onClick: function () {
            var node = ztree.getSelectedNodes();
            //选择上级菜单
            if (node[0].id.indexOf('AAA') !== -1 || node[0].id.indexOf('BBB') !== -1 || node[0].id.indexOf('CCC') !== -1) {
                vm.columnId = '';

                if (node[0].id.indexOf('AAA') !== -1) {
                    vm.q.storeType = '';
                }
                if (node[0].id.indexOf('BBB') !== -1) {
                    vm.q.storeType = 2;
                }
                if (node[0].id.indexOf('CCC') !== -1) {
                    vm.q.storeType = 1;
                }
            } else {
                vm.columnId = node[0].id;
            }
            vm.query();
        }
    }
};
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
};

var deptTree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        storeHouse: {},
        ruleValidate: {
            name: [
                {required: true, message: '仓库名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            storeCode: '',
            moduleType: '',
            storeType: ''
        },
        columnId: '',
        storeTypeList: enumsToList('STORE_TYPE'), //仓库类型
        moduleTypeList: enumsToList('STORE_TYPE_2'), //模块类型

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            //
            vm.showList = false;
            vm.title = "新增";
            vm.storeHouse = {id: '', delFlag: 1, workshopName: '',shortName:''};
            var total = 0;
            Ajax.request({
                url: "../storehouse/count?storeType=2",
                async: false,
                successCallback: function (r) {
                    total = r.total + 1;
                    vm.storeHouse.storeCode = 'INV_' + (Array(2).join(0) + total).slice(-2);
                }
            });
            vm.getDeptTree();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            ;
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getDeptTree();
        },
        saveOrUpdate: function (event) {
            var url = vm.storeHouse.id == (null || '') ? "../storehouse/save" : "../storehouse/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.storeHouse),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                        vm.getStore();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            // vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                // vm.storeHouse.delFlag = 0;
                Ajax.request({
                    url: "../storehouse/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                            vm.getStore();
                        });
                    }

                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../storehouse/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.storeHouse = r.storeHouse;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'storeCode': vm.q.storeCode,
                    'columnId': vm.columnId,
                    'storeType': vm.q.storeType,
                    'moduleType': vm.q.moduleType
                },
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                storeCode: '',
                moduleType: '',
                storeType: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        goStorePosition: function (id) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-location-arrow"></i> 库位',
                content: '../sys/storeposition.html?id=' + id
            })
        },
        goUser: function (id) {
            openWindow({
                type: 2,
                area: ['100%', '100%'],
                title: '<i class="fa fa-user"></i> 分管人员',
                content: '../sys/storeuser.html?id=' + id
            })
        },
        getStore: function () {
            //加载树
            Ajax.request({
                url: "../storehouse/allList",
                async: true,
                successCallback: function (r) {
                    var list = r.list;
                    var tree = [{id: 'AAA', name: '仓库', parentId: '0'},
                        {id: 'BBB', name: '线边库', parentId: 'AAA'},
                        {id: 'CCC', name: 'ERP库', parentId: 'AAA'}];
                    for (let i = 0; i < list.length; i++) {
                        var stoType = list[i].storeType;
                        if (stoType == "1") {
                            list[i].parentId = 'CCC'; //ERP库
                        } else {
                            list[i].parentId = 'BBB'; //线边库
                        }
                        tree.push(list[i]);
                    }
                    ztree = $.fn.zTree.init($("#storeTree"), setting, tree);
                }
            });
        },
        getDeptTree: function () {
            //加载分类树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=2',
                async: true,
                successCallback: function (r) {
                    deptTree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    deptTree.expandAll(true);
                    var node = deptTree.getNodeByParam("deptId", vm.storeHouse.workshop);
                    if (node) {
                        deptTree.selectNode(node);
                        vm.storeHouse.workshop = node.name;
                    } else {
                        node = deptTree.getNodeByParam("deptId", "0");
                        deptTree.selectNode(node);
                    }
                }
            });
        },
        deptTreeShow: function () {
            openWindow({
                title: "选择车间",
                area: ['300px', '450px'],
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = deptTree.getSelectedNodes();
                    vm.storeHouse.workshop = node[0].deptId;
                    vm.storeHouse.workshopName = node[0].name;
                    layer.close(index);
                }
            });
        },
    },
    created: function () {
       // this.getStore();
    }
});
